Previous Book Contents Book Index Next

Inside Macintosh: AppleScript Language Guide / Part 2 - AppleScript Language Reference
Chapter 3 - Values / Value Class Definitions


String

A value of class String is a character string (an ordered series of characters)
in AppleScript.

LITERAL EXPRESSIONS
Strings in scripts are always surrounded by quotation marks, as in these examples:

"string""Rolling along, stringing a song""Pennsylvania 68000"
To include quotation marks in a string, you must use the equivalent twocharacter sequence, \". For more information, see "Special Characters in Strings" later in this section.

PROPERTIES
Class
The class identifier for the object. This property is read-only, and its value is always string.
Length
The number of characters in the string.
ELEMENTS
Strings can have character, word, paragraph, and text elements.

The elements of a string may be different from the character, word, paragraph, and text objects of applications.

Character
A single character contained in the string.
Paragraph
A series of characters ending with either (1) a return character
or (2) the end of the string and beginning immediately after either (1) the first character after the end of the preceding paragraph or (2) the beginning of the string.
Text
A continuous series of characters, including spaces, tabs, and
all other characters, within a string (see "Notes" later in
this section).
Word
A continuous series of characters that contains only the following types of characters:
letters (including letters with diacritical marks)
digits
nonbreaking spaces
dollar signs, cent signs, English pound symbols, or yen symbols
percent signs
commas between digits
periods before digits
apostrophes between letters or digits
hyphens (but not minus signs [Option-hyphen] or dashes [Option-Shift-hyphen]).
Here are some examples of words:
non-functional
he's
v1.0
$99.99
12c-d
Note that this definition applies to English text in the Roman script system. Words in other languages are defined by the script system for each language if the appropriate script system is installed. (For more information about script systems, see page 317.)
OPERATORS
The operators that can have strings as operands are &, =, , >, , <,, Starts With, Ends With, Contains, Is Contained By, and As.

For detailed explanations and examples of how AppleScript operators treat strings, see "Operators That Handle Operands of Various Classes," which begins on page 168.

REFERENCE FORMS
You can use the following reference forms to refer to elements of strings:

You cannot use the Relative, Name, ID, or Filter reference forms.

SPECIAL CHARACTERS IN STRINGS
The backslash (\) and double-quote (") characters have special meaning in strings. If you want to include either of these characters in a string, you must use the equivalent two-character sequence:
Backslash character \\
Double-quote character\"

The tab and return characters can be included in strings, or they can be represented by equivalent two-character sequences:
Tab character\t
Return character\r

When a string containing any of the two-character sequences is displayed to the user (as, for example, in a dialog box), the sequences are converted. For example, the string

"item 1\t1\ritem 2\t2"
is displayed in a dialog box as

item 1   1
item 2   2
STRING CONSTANTS
AppleScript defines three constants for string values:
ConstantValue
space" "
tab"\t"
return"\r"

COERCIONS SUPPORTED
If a string consists of an appropriate number, AppleScript supports coercion of the string to an integer, a number, or a real number. Similarly, any integer, number, or real number can be coerced to a string. AppleScript also supports coercion of a string to a single-item list and coercion of a list whose items are all strings to a single concatenated string.

NOTES
There is no limit on the length of strings except the memory available in
the computer.

To get a contiguous range of characters within a string, use the text element. For example, the value of the following statement is the string "y thi".

get text of characters 3 thru 7 of "Try this at home"--result: "y thi"
The result of the same statement without the text element is a list.

get characters 3 thru 7 of "Try this at home"--result: {"y", " ", "t", "h", "i"}
You cannot set the value of an element of a string. For example, if you attempt to change the value of the first character of the string "boris" as shown in the following example, you'll get an error.

set myName to "boris"set character 1 of myName to "D"--results in an error, because you cannot set the values of 
--elements of strings 

Previous Book Contents Book Index Next

© Apple Computer, Inc.
13 JUL 1996